package com.sirap.fish.fetchers;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.google.common.collect.Lists;
import com.sirap.basic.component.Extractor;
import com.sirap.basic.tool.C;
import com.sirap.basic.tool.D;
import com.sirap.basic.util.Amaps;
import com.sirap.basic.util.Colls;
import com.sirap.basic.util.HttpUtil;
import com.sirap.basic.util.MathUtil;
import com.sirap.basic.util.StrUtil;
import com.sirap.common.domain.Album;
import com.sirap.extractor.images.MayFetcher;

public class MayImageFetchers {

	private static final int TOP_COUNT = 6;
	private static final Map<String, Object> TOP_LADIES = Amaps.newLinkHashMap();
	static {
		TOP_LADIES.put("易阳", 429);
		TOP_LADIES.put("赵伊彤", 652);
		TOP_LADIES.put("温心怡", 1249);
		TOP_LADIES.put("刘飞儿", 34);
		TOP_LADIES.put("妮可", 962);
		TOP_LADIES.put("闫盼盼", 804);
		TOP_LADIES.put("杨晨晨", 645);
		TOP_LADIES.put("巨乳", 1);
		TOP_LADIES.put("美女", 645);
		TOP_LADIES.put("熟女", 743);
		TOP_LADIES.put("性感", 893);
		TOP_LADIES.put("推女郎", 100);
		TOP_LADIES.put("尤果网", 488);
		TOP_LADIES.put("番号网", 653);
		TOP_LADIES.put("秀人", 602);
	}
	
	public static void main(String[] args) {
		Object va = null;
		String vb = null;
//		va = getAlbums(3);
//		D.pl(countOfAlbum("https://www.fenun.cn/Item/Details/20912"));
//		va = getAlbumsByPage(1);
		va = getAlbumsByPageUrl("http://www.699mm.com/new/index.html");
//		va = getAlbumsByPageUrl("http://www.699mm.com/search-0-429.html");
//		va = getAlbum("http://www.699mm.com/meitu/xiuren/87089.html");
//		vb = "http://www.699mm.com/zhubo/86290_15.html";
//		vb = "http://www.699mm.com/zhubo/86290.html";
//		vb = "http://www.699mm.com/search-0-1343.html";
//		va = getAlbumsByPageUrl(vb);
//		va = imagesInPage(vb);
//		va = tagsFromList(IOUtil.readLinesWithUTF8("C:/mastorage/may/all.words.txt"));
		D.pjsp(va);
	}
	
	public static Map<String, Object> getAlbumsByPageUrl(String aurl) {
		if(StrUtil.contains(aurl, "search")) {
			return getAlbumsBySearchUrl(aurl);
		} else {
			return getAlbumsByTypeUrl(aurl);
		}
	}
	
	private static Map<String, Object> getAlbumsBySearchUrl(String aurl) {
		Extractor<Map<String, Object>> frank = new Extractor<Map<String, Object>>() {
    		
			@Override
			public String getUrl() {
				showFetching();
				return aurl;
			}
			
			@Override
			protected void parse() {
				item = Amaps.newConcurrentHashMap();
				
				String r2 = "<font color='#cc3399'>\\s*(\\d+)\\s*</font>";
				String acount = StrUtil.findFirstMatchedItem(r2, source);
				
				int maxpage = 999;
				if(acount != null) {
					maxpage = MathUtil.totalPageOf(Integer.parseInt(acount), 20);
				}
				
				item.put("totalpage", maxpage);
				
				//<a href="search-1-1351.html" class="next">下一页</a>
				String r3 = "<a href=\"([^\"]+)\"[^<>]*>下一页</a>";
				String npage = StrUtil.findFirstMatchedItem(r3, source);
				if(npage != null) {
					if(!HttpUtil.isHttp(npage)) {
						npage = StrUtil.useDelimiter("/", "http://www.699mm.com", npage);
					}
					item.put("nextpage", npage);
				}
				
				String regex = "<ul[^<>]+class=\"clearfix\">(.+?)</ul>";
				String kids = StrUtil.findFirstMatchedItem(regex, source);
				Matcher ma = createMatcher("<li>(.+?)</li>", kids);
				while(ma.find()) {
					Map<String, Object> row = Amaps.newLinkHashMap();
					String kid = ma.group(1);
					String aurl = StrUtil.findFirstMatchedItem("<a href=\"([^\"]+)\"", kid);
					String cover = StrUtil.findFirstMatchedItem("<img src=\"([^\"]+)\"", kid);
					String title = StrUtil.findFirstMatchedItem("<span>(.+?)</span>", kid);
					String when = StrUtil.findFirstMatchedItem("<b class=\"b1\">([^<>]+)</b>", kid);

					row.put("aurl", aurl);
					row.put("cover", cover);
					row.put("title", getPrettyText(title));
					row.put("when", when);
					row.put("tags", tagsFromMap(TOP_LADIES));
					
					mexItems.add(row);
				}
				
				item.put("photos", mexItems);
				C.listSome(mexItems, 3);
			}
		};
		
		return frank.process().getItem();
	}
	
	public static Map<String, Object> tagsFromMap(Map<String, Object> data) {
		Map<String, Object> tags = Amaps.newLinkHashMap();
		String temp = "http://www.699mm.com/search-0-{0}.html";
		List<String> keys = Lists.newArrayList(data.keySet());
		Collections.shuffle(keys);
		keys = Colls.top(keys, TOP_COUNT);
		for(String key : keys) {
			tags.put(key, StrUtil.occupy(temp, data.get(key)));
		}
		
		return tags;
	}
	
	public static List<Map<String, Object>> tagsFromList(List<String> lines) {
		List<Map<String, Object>> tags = Lists.newArrayList();
		String regex = "(\\d+)\\s+(.+)";
		Pattern pa = Pattern.compile(regex);
		String temp = "http://www.699mm.com/search-0-{0}.html";
		Collections.shuffle(lines);
		for(String line : lines) {
			D.pl(line);
			Matcher ma = pa.matcher(line.trim());
			if(ma.find()) {
				String word = ma.group(2);
				if(StrUtil.isRegexMatched("\\d+", word)) {
					word = "A" + word;
				}
				String sid = ma.group(1);
				Map<String, Object> item = Amaps.createMap("word", word);
				item.put("url", StrUtil.occupy(temp, sid));
				tags.add(item);
			}
		}
		
		return tags;
	}
	
	private static Map<String, Object> getAlbumsByTypeUrl(String aurl) {
		Extractor<Map<String, Object>> frank = new Extractor<Map<String, Object>>() {
    		
			@Override
			public String getUrl() {
				showFetching();
				return aurl;
			}
			
			@Override
			protected void parse() {
				item = Amaps.newConcurrentHashMap();
				
				String r1 = "<span class=\"pageON\">总&nbsp;(\\d+)条记录 </span>";
				String acount = StrUtil.findFirstMatchedItem(r1, source);
				
				String r2 = "<a href=\"http://www.699mm.com/listinfo-\\d+-(\\d+).html\">尾页</a>";
				String pcount = StrUtil.findFirstMatchedItem(r2, source);
				D.pla(acount, pcount);
				
				int maxpage = 10;
				if(acount != null) {
					maxpage = MathUtil.totalPageOf(Integer.parseInt(acount), 25);
				} else {
					maxpage = Integer.parseInt(pcount) + 1;
				}
				
//				Map<String, Object> tags = TOP_LADIES;//tagsOf(source);
				
				item.put("totalpage", maxpage);
				
				String r3 = "<a href=\"([^\"]+)\"[^<>]*>下一页</a>";
				item.put("nextpage", StrUtil.findFirstMatchedItem(r3, source));
				
				String regex = "<ul class=\"clearfix\">(.+?)</ul>";
				String kids = StrUtil.findFirstMatchedItem(regex, source);
				Matcher ma = createMatcher("<li>(.+?)</li>", kids);
				while(ma.find()) {
					Map<String, Object> row = Amaps.newLinkHashMap();
					String kid = ma.group(1);
					String aurl = StrUtil.findFirstMatchedItem("<a href=\"([^\"]+)\"", kid);
					String cover = StrUtil.findFirstMatchedItem("lazysrc=\"([^\"]+)\"", kid);
					String title = StrUtil.findFirstMatchedItem("alt=\"([^\"]+)\"", kid);
					String when = StrUtil.findFirstMatchedItem("<b class=\"b1\">([^<>]+)</b>", kid);

					row.put("aurl", aurl);
					row.put("cover", cover);
					row.put("title", title);
					row.put("when", when);
					row.put("tags", tagsFromMap(TOP_LADIES));
					
					mexItems.add(row);
				}
				
				item.put("photos", mexItems);
				C.listSome(mexItems, 3);
			}
		};
		
		return frank.process().getItem();
	}
	
	public static Album getAlbum(String albumurl) {
		Album bum = MayFetcher.of(albumurl);
		
		return bum;
	}
	
	public static String countOfImages(String albumurl) {
		Extractor<String> frank = new Extractor<String>() {
    		
			@Override
			public String getUrl() {
				showFetching();
				return albumurl;
			}
			
			@Override
			protected void parse() {
				String regex = "<span id=\"allnum\">(\\d+)</span>";
				item = StrUtil.findFirstMatchedItem(regex, source);
			}
		};
		
		return frank.process().getItem();
	}
	
	public static Map<String, Object> countAndTitleOf(String albumurl) {
		Extractor<Map<String, Object>> frank = new Extractor<Map<String, Object>>() {
    		
			@Override
			public String getUrl() {
				showFetching();
				return albumurl;
			}
			
			@Override
			protected void parse() {
				item = Amaps.newConcurrentHashMap();
				String regex = "<span id=\"allnum\">(\\d+)</span>";
				item.put("count", StrUtil.findFirstMatchedItem(regex, source));

				regex = "alt=\"([^\"]+)\"";
				item.put("title", StrUtil.findFirstMatchedItem(regex, source));
			}
		};
		
		return frank.process().getItem();
	}
}
